package leetcode;

/**
 * dp
 * 回溯都可以
 */
public class lc_377 {
    static int num;
    public static void main(String[] args) {
        System.out.println(combinationSum4(new int[]{1, 2, 3}, 4));
        System.out.println(combinationSum4_2(new int[]{1, 2, 3}, 4));
    }
    public static int combinationSum4(int[] nums, int target) {
        int n = nums.length;
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for(int i = 1; i <= target; i++){
            for(int j = 0; j < n; j++){
                if(i >= nums[j]){
                    dp[i] += dp[i - nums[j]];
                }
            }
        }
        return dp[target];
    }
    public static int combinationSum4_2(int[] nums, int target) {
        dfs(nums, target, 0);
        return num;
    }
    public static void dfs(int[] nums, int target, int sum){
        if(sum > target)
            return;
        if(sum == target){
            num++;
            return;
        }
        for(int i = 0; i < nums.length; i++){
            dfs(nums, target,sum + nums[i]);
        }
    }
}
